package com.pig4cloud.pig.admin.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.admin.api.entity.SysLog;
import com.pig4cloud.pig.admin.api.entity.WebIndexEntity;

import com.pig4cloud.pig.admin.api.vo.WebIndexQuery;
import com.pig4cloud.pig.admin.mapper.WebIndexMapper;
import com.pig4cloud.pig.admin.service.IWebIndexService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class WebIndexServiceImpl extends ServiceImpl<WebIndexMapper, WebIndexEntity> implements IWebIndexService {

	@Autowired
	private WebIndexMapper webIndexMapper;

	@Override
	public Page getPage(Page page, WebIndexQuery webIndexQuery) {
		LambdaQueryWrapper<WebIndexEntity> wrapper = Wrappers.lambdaQuery();
		if (StrUtil.isNotBlank(webIndexQuery.getWebName())) {
			wrapper.eq(WebIndexEntity::getWebName, webIndexQuery.getWebName());
		}

		if (StrUtil.isNotBlank(webIndexQuery.getWebUrl())) {
			wrapper.eq(WebIndexEntity::getWebUrl, webIndexQuery.getWebUrl());
		}

		if (StrUtil.isNotBlank(webIndexQuery.getCreateName())) {
			wrapper.eq(WebIndexEntity::getCreateName, webIndexQuery.getCreateName());
		}

		return webIndexMapper.selectPage(page,wrapper);
	}

    @Override
    public boolean add(WebIndexEntity webIndexEntity) {
		WebIndexEntity vo = new WebIndexEntity();
		BeanUtils.copyProperties(webIndexEntity,vo);
		boolean res = retBool(this.webIndexMapper.insert(vo));
		return res;
    }

    @Override
    public boolean delete(Integer id) {
        return retBool(this.webIndexMapper.deleteById(id));

    }

	@Override
	public boolean edit(WebIndexEntity webIndexEntity) {
		return this.updateById(webIndexEntity);
	}

	@Override
	public WebIndexEntity viewIndex(Integer id) {
		QueryWrapper<WebIndexEntity> wrapper = new QueryWrapper<>();
		wrapper.eq("id",id);
		return this.webIndexMapper.selectOne(wrapper);
	}
}
